package com.cls.system.service.impl;

import com.cls.common.entity.QueryRequest;
import com.cls.system.entity.Template;
import com.cls.system.mapper.TemplateMapper;
import com.cls.system.service.ISysFileService;
import com.cls.system.service.ITemplateService;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import lombok.RequiredArgsConstructor;
import com.github.pagehelper.PageHelper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
import java.util.List;

/**
 * 模板管理 Service实现
 *
 * @author slx
 * @date 2020-11-24 15:33:34
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class TemplateServiceImpl extends ServiceImpl<TemplateMapper, Template> implements ITemplateService {
    private final ISysFileService sysFileService;
    @Override
    public PageInfo<Template> findTemplates(QueryRequest request, Template template) {

        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<Template> list= this.baseMapper.findPage(template);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public List<Template> findTemplates(Template template) {
	    LambdaQueryWrapper<Template> queryWrapper = new LambdaQueryWrapper<>();
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createTemplate(Template template) {
        this.save(template);
        sysFileService.updateIsFinish(template.getFileId(),null);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateTemplate(Template template) {
        //查询原来数据
        Template byId = this.getById(template.getTemplateId());
        this.saveOrUpdate(template);
        sysFileService.updateIsFinish(template.getFileId(),byId.getFileId());

    }


    /**
    * 删除
    * @param ids
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteTemplates(String[] ids) {
        List< String > list = Arrays.asList(ids);
        this.removeByIds(list);
     }

    @Override
    public Template findByTemplateTypeCode(String templateTypeCode){
        return baseMapper.findByTemplateTypeCode(templateTypeCode);
    }

}
